package com.yinhe.overtime.view;

import com.yinhe.overtime.common.NumberTextField;
import com.yinhe.overtime.object.OverTime;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class MainView {
    private JButton clearButton;
    private JList overTimeList;
    private JLabel hourLabel;
    private JLabel minuteLabel;
    private JLabel ListLabel;
    private JPanel jPanel;
    private JButton calcButton;
    private JTextField hourTextField;
    private JTextField minuteTextField;
    private JLabel sumOverTimeLabel;
    private Vector<OverTime> overTimes;
    private OverTime sumOverTime = null;

    public MainView() {
        overTimes = new Vector<OverTime>();
        sumOverTime = new OverTime(0, 0);
        initView();
    }

    private void initView() {
        hourTextField.setDocument(new NumberTextField());
        minuteTextField.setDocument(new NumberTextField());
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearView();
            }
        });
        calcButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OverTime overTime = getNewOverTime();
                calcSum(overTime);
                updateView(overTime);
                hourTextField.setText("");
                minuteTextField.setText("");
            }
        });
    }

    public void show() {
            JFrame frame = new JFrame("OverTime");
        frame.setContentPane(new MainView().jPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setSize(400, 500);
//        Toolkit toolkit = Toolkit.getDefaultToolkit();
//        int x = (int) (toolkit.getScreenSize().getWidth() - frame.getWidth()) / 2;
//        int y = (int) (toolkit.getScreenSize().getHeight() - frame.getHeight()) / 2;
//        frame.setLocation(x, y);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void calcSum(OverTime overTime) {
        sumOverTime.add(overTime);
    }

    private OverTime getNewOverTime() {
        String hourString = hourTextField.getText();
        String minuteString = minuteTextField.getText();
        hourString = ("".equals(hourString) || null == hourString) ? "0" : hourString;
        minuteString = ("".equals(minuteString) || null == minuteString) ? "0" : minuteString;
        int hour = Integer.valueOf(hourString.trim());
        int miunte = Integer.valueOf(minuteString.trim());
        OverTime overTime = new OverTime(hour, miunte);
        return overTime;
    }

    private void updateView(OverTime overTime) {
        overTimes.add(overTime);
        overTimeList.setListData(overTimes);
        sumOverTimeLabel.setText(sumOverTime.toString());
    }

    private void clearView() {
        hourTextField.setText("");
        minuteTextField.setText("");
        sumOverTime.setMinute(0);
        sumOverTime.setHour(0);
        sumOverTimeLabel.setText(sumOverTime.toString());
        overTimes.clear();
        overTimeList.setListData(overTimes);
    }
}
